﻿using UnityEngine;
using UnityEditor;
using UnityEditor.Extension;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Extension;
using System;

namespace UnityEngine.Attributes
{
	[CustomPropertyDrawer(typeof(ModuleAttribute)), CanEditMultipleObjects]
	public class ModularAttributeDrawer : PropertyDrawer
	{	
		static GUISkin _skin = null;
		public static GUISkin skin 
		{	 get  {
				if(_skin == null)
            {
               string skinPath = "Assets/Tools/Attributes/Editor/Assets/ModuleSkin.guiskin";
               _skin = (GUISkin)Resources.LoadAssetAtPath(skinPath, typeof(GUISkin));
            }
				return _skin;  }
		}

		int propertyHeight = 15; // 15 - pixel count represents height of each inspector property drawn
		private ModuleAttribute attributeValue { get { return (ModuleAttribute)attribute;} }
		public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
		{
			if(skin != null)
				GUI.skin = skin;
            Action<Rect, SerializedProperty, GUIContent>[] actions = new Action<Rect, SerializedProperty, GUIContent>[] {
                    PrimaryGUI, ModularGUI };
            int index = property.propertyType == SerializedPropertyType.Generic ? 1 : 0; // indexer to 'actions' array
            actions[index](position, property, label);
		}
		
        void ModularGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            //Init
			SerializedProperty enabledProperty = property.FindPropertyRelative("enabled");
            Rect cursorRect = new Rect(position);
            cursorRect.height = propertyHeight;
            Rect htRect = new Rect(cursorRect);
            htRect.x += 15;
            property.isExpanded = GUI.Toggle(htRect, property.isExpanded, attributeValue.name, GUI.skin.button);
			if(enabledProperty!= null)
            {
                Rect tRect = new Rect(cursorRect);
                tRect.width = 15;
                enabledProperty.boolValue = GUI.Toggle(tRect, enabledProperty.boolValue, "");
            }

            //Folder
            if(property.isExpanded)
            property.ForEachVisibleProperty(f =>
            {
				if(f.name != "enabled")
				{
                	cursorRect.y += propertyHeight;
                	EditorGUI.PropertyField(cursorRect, f);
				}
            });
			GUI.Label(position,"");
        }

        void PrimaryGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.PropertyField(position, property, label);
        }

		public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
		{
            int recCount = 1;
            int nCount = property.isExpanded ? property.VisiblePropertyCount() : recCount;
            return nCount * propertyHeight;
		}
	}
}		
//		EditorGUI.LabelField(new Rect(position.x + buttonWidth + 40, position.y, position.width - (buttonWidth * 2 + 80), position.height), property.name + ": " + valueString);